package com.demo.test.graph;

import java.util.HashMap;
import java.util.Map;

public class Graph {
    private Map<String, Node> nodes;  // 存储所有节点，键是节点的标识符

    public Graph() {
        this.nodes = new HashMap<>();
    }

    public Node getNode(String id) {
        return nodes.get(id);
    }

    public void addNode(Node node) {
        nodes.put(node.getId(), node);
    }

    public void addEdge(String fromId, String toId) {
        Node fromNode = nodes.get(fromId);
        Node toNode = nodes.get(toId);
        if (fromNode != null && toNode != null) {
            fromNode.addNeighbor(toNode);
        }
    }

    public void printGraph() {
        for (Node node : nodes.values()) {
            System.out.print(node.getId() + " -> ");
            for (Node neighbor : node.getNeighbors()) {
                System.out.print(neighbor.getId() + " ");
            }
            System.out.println();
        }
    }
}
